<!--  -->
<template>
  <div class="grammer-study">
    <div class="chose-course-top">
      <div class="header-left">
        <span
          class="back-arrow"
          @click="() => this.$router.push('/study/grammar?ref=homepage&userId='+this.reverseStatus.userId)"
        ></span>
      </div>
      <div class="course-title">
        <span v-if="this.$route.query.type === '0'">今日学习</span>
        <span v-if="this.$route.query.type === '1'">累计学习</span>
        <span v-if="this.$route.query.type === '2'">句子总数</span>
      </div>
      <div class="btn-group">
        <a  @click="print">打印</a>
      </div>
    </div>
    <div class="list-wrap" v-if="learnList.length">
      <studyList  :renderData="learnList"></studyList>
    </div>
    <div style="display: none">
      <div class="list-wrap"  ref="contentToExport">
        <studyList2 :renderData="learnList"></studyList2>
      </div>
    </div>
  </div>
</template>

<script>
import studyList from "./studyList";
import studyList2 from "./studyList2";
import {mapState} from "vuex";
import {grammarCourseAll, grammarCourseDay, grammarCourseVocabulary} from "@/api/reverse";
import html2pdf from "html2pdf.js";
export default {
  name: "grammarStudy",
  components: {
    studyList,
    studyList2,

  },
  data() {
    return {
      learnList: [],
      title:'',
    };
  },
  computed: {
    ...mapState(["reverseStatus"]),
  },
  watch: {},
  methods: {
    print(){
      const element = this.$refs.contentToExport;
      const options = {
        margin:       1,
        filename:     `${this.title}.pdf`,
        image:        { type: 'jpeg', quality: 0.98 },
        html2canvas:  { dpi: 192, letterRendering: true },
        jsPDF:        { unit: 'mm', format: 'a4', orientation: 'portrait' }
      };
      html2pdf()
          .from(element)
          .set(options)
          .save();
    },

  },
  async created() {
    let data ={
      courseId:this.reverseStatus.curr_id,
      userId:this.reverseStatus.userId,
      packageId:this.reverseStatus.packageId,
    }
    if (this.$route.query.type === "0"){
      this.title = '今日学习'
      await grammarCourseDay(data).then((res)=>{
        this.learnList = res.data.map((v) => ({
          ...v,
          clickNum: 0
        }));
      })
    }
    if (this.$route.query.type === "1"){
      this.title = '累计学习'
      await grammarCourseAll(data).then((res)=>{
        this.learnList = res.data.map((v) => ({
          ...v,
          clickNum: 0
        }));
      })
    }
    if (this.$route.query.type === "2"){
      this.title = '句子总数'
      await grammarCourseVocabulary(data).then((res)=>{
        this.learnList = res.data.map((v) => ({
          ...v,
          clickNum: 0
        }));
      })
    }
    // this.learnList = this.learnList.map((v) => ({
    //   ...v,
    //   clickNum: 0
    // }));
    // this.fetchLearningList();
  },
  mounted() {},
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  // beforeRouteLeave(to, from, next) {
  //   fetchEndTime(this.$route.query);
  //   next();
  // },
};
</script>
<style scoped>
.grammer-study {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;

  .list-wrap {
    flex: 1;
    overflow-y: auto;
    background: #F0F2F4;
    padding: 30px 40px;
  }
  .chose-course-top {
    text-align: center;
    display: flex;
    justify-content: space-between;
    position: relative;
    align-items: center;
    height: 80px;
    padding: 0 40px;
    background-color: #fff;
    .anticon {
      font-size: 24px; /* no */
    }

    .header-left {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      .back-arrow {
        width: 30px;
        height: 27px;
        background-image: url("@/assets/home/btn_back.png");
        background-size: 100%;
        cursor: pointer;
      }
    }

    .course-title {
      width: 100%;
      flex: 1;
      font-size: 20px;
      font-weight: 700;

      .check-title {
        color: #c7c7c7;
      }
    }
    .btn-group {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      & > a {
        color: #27BFA3;
        font-size: 20px;
        display: inline-block;
        margin-left: 50px;
      }
      .play-text {
        font-weight: 700;
      }
    }
  }
}
</style>
